_pos = _this select 0;
_aiSide = _this select 1;


if ((_aiSide == side0 AND !Side0Bought) OR  (_aiSide == side1 AND !Side1Bought)) then
{
	if(_aiSide == side0) then { _money = side0_money;_ic = side0_ic;_lc = side0_lc; _options = side0CommanderMenu; _aiMakerColor = side0_MarkerColor;};
	if(_aiSide == side1) then { _money = side1_money;_ic = side1_ic;_lc = side1_lc; _options = side1CommanderMenu; _aiMakerColor = side1_MarkerColor;};
	_bought = false;

	/* 
		Find out nearest base to deploy from.
		Adjust the positon so the AI doesn't move directly towards the flag. Needs to be more sneaky.
	*/

	_base = [_pos, _aiSide] call fGetNearestFOB;

	_x = random 500 - random 500;
	_y = random 500 - random 500;
	_pos = [(getpos _pos select 0) + _x, (getpos _pos select 1) +_y,0];

	if(_aiSide == side0) then { _AAtype = side0_AAVeh; _ATtype = side0_ATVeh; };
	if(_aiSide == side1) then { _AAtype = side1_AAVeh; _ATtype = side1_ATVeh; };

	_aadef = nearestObjects [_pos, [_AAtype], 1500];
	_atdef = nearestObjects [_pos, [_ATtype], 1500];
	_buildAA = true;
	_buildAT = true;

	if (count _aadef > 2) then { _buildAA = false; };
	if (count _atdef > 2) then { _buildAt = false; };

	if (_buildAA AND _buildAT AND DEV) then {  player sidechat "Time to force ai to build def in diffrent city but guess what not implemented";};

	if((_money > 400) AND (_ic > 970) AND (_lc > 500) AND !(PreferAir)) AND (random 4 > 2) then { _bought = true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_CASplane.sqs"; _xmoney = 0; _xic = 950; _xLC = 0; };
	if((_money > 400) AND (_ic > 970) AND (_lc > 300) AND !(PreferAir)) then { _bought = true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_chopper.sqs"; _xmoney = 0; _xic = 950; _xLC = 0; };
	if((_money > 200) AND (_ic > 960) AND (_lc > 90) AND (_buildAT)) then { _bought = true"; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_AT.sqs"; _xmoney = 0; _xic = 950; _xLC = 0; };
	if((_money > 700) AND (_ic > 200) AND (_lc > 15) AND (PreferAir)) then { _bought = true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_sam.sqs"; _xmoney = 700; _xic = 200; _xLC = 15; };
	if((_money > 400) AND (_ic > 400) AND (_lc > 90) AND (_buildAA)) then { _bought = true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_AA.sqs"; _xmoney = 50; _xic = 400; _xLC = 90; };
	if((_money > 400) AND (_ic > 40) AND (_lc > 20)) then { [_pos,_aiSide] exec "server\coop\bought\commanderOrdered_SF.sqs"; _xmoney = 200; _xic = 10; _xLC = 20; };

	
	if((_money > 200) AND (_ic > 100) AND (_lc > 70)  AND !(PreferTanks) AND (random 5 > 3)) then { _bought = true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_frt.sqs"; _xmoney = 200; _xic = 30; _xLC = 70;};

	if (_bought) then
	{
			/*
				Make the AI pay and wait a bit so he doesn't spam units.
			*/
		if (_aiSide == side0) then { Side0Bought = true;};
		if (_aiSide == side1) then { Side1Bought = true;};
		if (_aiSide == side0) then { side0_money = side0_money - _xmoney; side0_ic = side0_ic - _xic; side0_lc = side0_lc - _xLC; };
		if (_aiSide == side1) then { side1_money = side1_money - _xmoney; side1_ic = side1_ic - _xic; side1_lc = side1_lc - _xLC; };
		sleep (random 60);

		if (_aiSide == side0) then { Side0Bought = false;};
		if (_aiSide == side1) then { Side1Bought = false;};
	};
};

